package com.kdtech.analyse.video;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONObject;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.analyse.AnalyseNews;

public class PPTVAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		// http://v.pptv.com/show/JtGFAxgrldM2tBw.html
		if(url.indexOf("#")!=-1){
			url = StringUtils.substringBefore(url, "#");
		}else if(url.indexOf("?")!=-1){
			url = StringUtils.substringBefore(url, "?");
		}
		String[] regex = { "http://v.pptv.com/show/.*.html" };
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (!isDetailPage(urlMeta.getUrl())) {
		}
		NewsMeta video = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		video.setUrl(url);
		String title = null;
		String desc = null;
		Long date = null;

		Document doc = Jsoup.parse(html);
		
		/*
		 * 解析标题
		 */
		
			title = doc.select("title").text();
			title = StringUtils.substringBefore(title, "_");
			if(StringUtils.isBlank(title)){
			title = doc.select("div.module-video-title dl.cf dd h3").text();
		}
		/*
		 * 解析时间
		 */
		Elements select = doc.select("p.info span:containsOwn(发布时间：) em");
		String vid = StringUtils.substringBetween(html, "var webcfg = {\"id\":", ",\"");
		String detailUrl="http://svcdn.pptv.com/show/v2/meta.json?cid="+vid;
		if(StringUtils.isNotBlank(vid)){
			UrlMeta responseToURL = CrawlHTML.responseToURL(detailUrl);
			if(responseToURL!=null){
				String html2 = responseToURL.getHtml();
				if(StringUtils.isNotBlank(html2)){
					try {
						JSONObject json = JSONObject.parseObject(html2).getJSONObject("data");
						if(StringUtils.isBlank(title)){
							if(json.containsKey("title")){
								title = json.getString("title");
							}						}
						if(json.containsKey("publish_time")){
							date = DateUtils.matchDate(json.getString("publish_time"));
						}
						if(json.containsKey("summary")){
							desc = json.getString("summary");
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}

		if (StringUtils.isBlank(title)||date==null) {
		}
		video.setTitle(title);
		video.setDate(date);
//		video.setContent(desc);
		return video;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	public static void main(String[] args) {
		PPTVAnalyse a = new PPTVAnalyse();
		String url = "http://v.pptv.com/show/dIvjYJwQ2hh7ibWE.html";
		if (a.isDetailPage(url)) {
			UrlMeta meta = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = a.parserHtml(meta);
			System.out.println(parserHtml);
		} else {
			System.out.println("不匹配规则");
		}

	}

	
}
